package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author SpongeBob
 */
@Service
@RequiredArgsConstructor
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {
    private final IAgencyCertificationService agencyCertificationService;

    /**
     * 添加机构认证申请
     *
     * @param agencyCertificationUpdateDTO 机构认证申请信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAgency(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        Long userId = UserContext.currentUserId();
        AgencyCertification agencyCertification = BeanUtil.toBean(agencyCertificationUpdateDTO, AgencyCertification.class);
        agencyCertification.setId(userId);
        agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        agencyCertificationService.saveOrUpdate(agencyCertification);

        AgencyCertificationAudit agencyCertificationAudit = BeanUtil.toBean(agencyCertificationUpdateDTO, AgencyCertificationAudit.class);
        agencyCertificationAudit.setAuditStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        agencyCertificationAudit.setServeProviderId(userId);
        this.saveOrUpdate(agencyCertificationAudit);


    }

    /**
     * 查询当前用户最新的驳回原因
     *
     * @return 驳回原因
     */
    @Override
    public RejectReasonResDTO queryCurrentUserLastRejectReason() {
        Long userId = UserContext.currentUserId();
        //根据id查询机构认证审核信息
        AgencyCertificationAudit agencyCertificationAudit = this.getOne(Wrappers.<AgencyCertificationAudit>lambdaQuery().eq(AgencyCertificationAudit::getServeProviderId, userId));
        String rejectReason = agencyCertificationAudit.getRejectReason();
        return new RejectReasonResDTO(rejectReason);

    }

    /**
     * 机构认证审核列表
     *
     * @param agencyCertificationAuditPageQueryReqDTO 机构认证审核列表查询参数
     * @return 机构认证审核列表
     */
    @Override
    public PageResult<AgencyCertificationAuditResDTO> pageQueryAgencyCertificationAudit(AgencyCertificationAuditPageQueryReqDTO agencyCertificationAuditPageQueryReqDTO) {
        return PageHelperUtils.selectPage(agencyCertificationAuditPageQueryReqDTO,
                () -> baseMapper.pageQueryAgencyCertificationAudit(agencyCertificationAuditPageQueryReqDTO));
    }

    /**
     * 机构认证审核
     *
     * @param id                       机构认证审核id
     * @param certificationAuditReqDTO 机构认证审核参数
     */
    @Override
    @Transactional
    public void audit(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {
        // 查询机构认证审核信息
        AgencyCertificationAudit agencyCertificationAudit = getById(id);

        if (agencyCertificationAudit == null) {
            throw new RuntimeException("机构认证审核信息不存在");
        }
        Long Pid = agencyCertificationAudit.getServeProviderId();
        AgencyCertification agencyCertification = agencyCertificationService.getById(Pid);
        // 获取当前登录用户信息
        Long currentUserId = UserContext.currentUserId();
        String currentUserName = UserContext.currentUser().getName();

        // 更新审核状态和认证状态
        agencyCertificationAudit.setAuditStatus(1);
        agencyCertification.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        agencyCertificationAudit.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        agencyCertificationAudit.setAuditTime(LocalDateTime.now());
        agencyCertificationAudit.setAuditorId(currentUserId);
        agencyCertificationAudit.setAuditorName(currentUserName);

        // 如果认证失败，设置驳回原因
        if (certificationAuditReqDTO.getCertificationStatus() == 3) {
            agencyCertificationAudit.setRejectReason(certificationAuditReqDTO.getRejectReason());
        }

        // 更新数据库
        updateById(agencyCertificationAudit);
        agencyCertificationService.updateById(agencyCertification);
    }
}
